<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="selection.length == 0"
          @click="batch_del"
        ></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="search.keyword"
            placeholder="部门名称"
            clearable
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <el-table
        v-loading="loading"
        :data="tableData"
        row-key="id"
        :tree-props="{
          hasChildren: 'fIsChildren',
        }"
        lazy
        :load="loadList"
        border
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <!-- <el-table-column label="序号" prop="id" width="120" /> -->
        <el-table-column
          label="单位名称"
          prop="cDepartmentName"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="上级单位"
          prop="cParentDepartmentName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="单位编号"
          prop="cDepartmentNo"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="浙政钉组织"
          prop="cOrganizationName"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="创建人" prop="cCreateUser" />

        <el-table-column
          label="创建时间"
          align="center"
          :key="3"
          prop="createTime"
        >
          <template #default="scope">
            <!-- <span>{{ parseTime(scope.row.tCreateDate) }}</span> -->
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="200"
          fixed="right"
        >
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button text type="primary" size="small">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>

  <save-dialog
    v-if="dialog.save"
    ref="saveDialog"
    :deptOptions2="deptOptions2"
    @success="handleSaveSuccess"
    @closed="dialog.save = false"
  ></save-dialog>
</template>

<script>
import saveDialog from "./save.vue";

export default {
  name: "dept",
  components: {
    saveDialog,
  },
  data() {
    return {
      maps: new Map(),
      dialog: {
        save: false,
      },
      tableData: [],
      deptOptions2: [],
      loading: false,
      // apiObj: this.$API.system.dept.GetDepartmentList,
      selection: [],
      search: {
        keyword: null,
      },
    };
  },
  async created() {
    let arr = await this.getDepartmentPages();

    console.log("children", arr);

    this.tableData = arr;
    this.getDpListTree();
  },
  methods: {
    // 局部刷新列表
    refreshRow(row) {
      const { tree, treeNode, resolve } = this.maps.get(row.fParentId);
      this.loadList(tree, treeNode, resolve);
    },
    async getDpListTree() {
      const { content } = await this.$API.system.user.GetDepartmentList.get();
      this.deptOptions2 = [
        { id: 0, cDepartmentName: "顶级", children: content.list },
      ];
    },
    async table_del(row) {
      var reqData = { ids: [row.id] };
      var res = await this.$API.system.dicts.deleteConfigType.post(reqData);
      if (res.code == 10000) {
        this.$refs.table.refresh();
        this.$message.success("删除成功");
      } else {
        this.$alert(res.message, "提示", { type: "error" });
      }
    },
    async getDepartmentPages(fParentId) {
      const { content } = await this.$API.system.dept.GetDepartmentPages.get({
        fParentId: fParentId || 0,
        pageSize: 999,
      });
      return content.list;
    },
    async loadList(tree, treeNode, resolve) {
      console.log(66666);
      this.loading = true;
      this.maps.set(tree.id, { tree, treeNode, resolve });

      let list = await this.getDepartmentPages(tree.id);
      this.loading = false;

      const copyList = list.filter(
        (item) => item.cDepartmentName !== tree.cDepartmentName
      );
      resolve(copyList);
    },
    //添加
    add() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open();
      });
    },
    //编辑
    table_edit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //查看
    table_show(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("show").setData(row);
      });
    },
    //删除
    async table_del(row) {
      var reqData = { ids: [row.id] };
      var res = await this.$API.system.dept.DeleteInfo.post(reqData);
      if (res.code == 10000) {
        // this.$refs.table.refresh();
        this.refreshRow(row);
        this.$message.success("删除成功");
      } else {
        this.$alert(res.message, "提示", { type: "error" });
      }
    },
    //批量删除
    async batch_del() {
      this.$confirm(
        `确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`,
        "提示",
        {
          type: "warning",
        }
      )
        .then(() => {
          const loading = this.$loading();
          this.$refs.table.refresh();
          loading.close();
          this.$message.success("操作成功");
        })
        .catch(() => {});
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
    //搜索
    upsearch() {},
    //根据ID获取树结构
    filterTree(id) {
      var target = null;
      function filter(tree) {
        tree.forEach((item) => {
          if (item.id == id) {
            target = item;
          }
          if (item.children) {
            filter(item.children);
          }
        });
      }
      filter(this.$refs.table.tableData);
      return target;
    },
    //本地更新数据
    handleSaveSuccess(data, mode) {
      this.refreshRow(data);
      // this.$refs.table.refresh();
    },
  },
};
</script>

<style></style>
